package com.llb666.bigdata;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class HdfsOp {
    public static void main(String[] args) throws IOException {
        // 配置 hdfs 地址
        Configuration conf  =new Configuration();
        conf.set("fs.defaultFS","hdfs://localhost:8020");
        // 获取操作hdfs对象
        FileSystem fileSystem = FileSystem.get(conf);
        //上传文件
        hdfsPut(fileSystem);
        // 下载文件
//        hdfsGet(fileSystem);
        // 删除文件
//        hdfsDel(fileSystem);
    }

    /**
     *  上传.
     */
    public static void hdfsPut(FileSystem fileSystem) throws IOException {
        // 本地文件输入流
        FileInputStream fis =
                new FileInputStream("/Users/mac/ProgramFiles/elk/logstash-7.9.1/bin/mysql/jdbc-foodie.sql");
        // hdfs输出流
        FSDataOutputStream fos = fileSystem.create(new Path("/fis_liulb_dml.sql"));
        IOUtils.copyBytes(fis,fos,1024,true);
    }

    /**
     * 下载.
     */
    public static void hdfsGet(FileSystem fileSystem) throws IOException {
        // hdfs输入流
        FSDataInputStream fis = fileSystem.open(new Path("/fis_liulb_dml.sql"));
        // 本地文件输出流
        FileOutputStream fos = new FileOutputStream("/Users/mac/fis_liulb_dml.sql");
        IOUtils.copyBytes(fis,fos,1024,true);
    }


    // 删除文件或者目录.
    public static void hdfsDel(FileSystem fileSystem) throws IOException {
        boolean isDeleted = fileSystem.delete(new Path("/fis_liulb_dml.sql"), true);
        if(isDeleted){
            System.out.print("删除成功");
        }else {
            System.out.print("删除失败");
        }
    }
}
